﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class AdminDet : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
        }
    }

    private void Bind()
    {
        string sql = string.Format("select * from Department");
        DataSet ds = DBHelper.GetDataSet(sql);
        this.GridView1.DataSource = ds;
        this.GridView1.DataKeyNames = new string[] { "ID" };
        this.GridView1.DataBind();        
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        Bind();
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        Bind();
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int id = Convert.ToInt32(this.GridView1.DataKeys[e.RowIndex].Value);
        TextBox Number1 = (TextBox)this.GridView1.Rows[e.RowIndex].FindControl("TextBox2");
        TextBox Name1 = (TextBox)this.GridView1.Rows[e.RowIndex].FindControl("TextBox3");
        TextBox Den1 = (TextBox)this.GridView1.Rows[e.RowIndex].FindControl("TextBox4");
        string Number = Number1.Text;
        string Name = Name1.Text;
        string Den = Den1.Text;
        string sql = string.Format("update Department set Number='{0}',Name='{1}',Description='{2}' where id={3}",Number,Name,Den,id);
        DBHelper.GetNonQuery(sql);
        this.GridView1.EditIndex = -1;
        Bind();
    }


    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        Bind();
    }


    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
        string sql = string.Format("delete from Department where id='{0}'", id);
        int n = DBHelper.GetNonQuery(sql);
        if (n <= 0)
        {
            Response.Write("<script>alert('删除失败！')</script>");
        }
        Bind();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[5].Attributes.Add("onclick", "return confirm('确定要删除吗')");
            //高亮显示
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
        }
    }
}